package schedule;

import java.util.Calendar;
import java.util.Date;

public class Period {
	public static boolean isInPayPeriod(Date actual, Date start, Date end) {
		if ((actual.compareTo(start) >= 0) && (actual.compareTo(end) <= 0)) {
			return true;
		} else {
			return false;
		}
	}

	public static int getNumberOfFridaysInPayPeriod(Date start, Date end) {
		Calendar startCal;
		Calendar endCal;
		startCal = Calendar.getInstance();
		startCal.setTime(start);
		endCal = Calendar.getInstance();
		endCal.setTime(end);
		int fridays = 0;

		// Return 0 if start and end are the same
		if (startCal.getTimeInMillis() == endCal.getTimeInMillis()) {
			return 0;
		}

		if (startCal.getTimeInMillis() > endCal.getTimeInMillis()) {
			startCal.setTime(end);
			endCal.setTime(start);
		}

		do {
			startCal.add(Calendar.DAY_OF_MONTH, 1);
			if (startCal.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY) {
				++fridays;
			}
		} while (startCal.getTimeInMillis() < endCal.getTimeInMillis());

		return fridays;
	}

	public static Date addDays(int num, Date day) {
		long oneDay = 1 * 24 * 60 * 60 * 1000;
		day.setTime(day.getTime() + (num * oneDay));
		return day;
	}
}
